package fr.neticar.webremoteclient.client;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.FocusPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.KeyboardListener;
import com.google.gwt.user.client.ui.MouseListener;
import com.google.gwt.user.client.ui.Widget;

public class ImagePanel extends FocusPanel {

    private AbsolutePanel container = new AbsolutePanel();

    private List listeners = new ArrayList();

    public ImagePanel() {
        super();
        add(this.container);
        initMouseListener();
        initKeyboardListener();
    }

    private void initMouseListener() {
        addMouseListener(new MouseListener() {

            public void onMouseDown(Widget sender, int x, int y) {
                MouseEventBean event = new MouseEventBean(x, y, EventType.ON_MOUSE_DOWN);
                fireMouseEvent(event);
            }

            public void onMouseEnter(Widget sender) {
                // Nothing to do here
            }

            public void onMouseLeave(Widget sender) {
                // Nothing to do here
            }

            public void onMouseMove(Widget sender, int x, int y) {
                MouseEventBean event = new MouseEventBean(x, y, EventType.ON_MOUSE_MOVE);
                fireMouseEvent(event);
            }

            public void onMouseUp(Widget sender, int x, int y) {
                MouseEventBean event = new MouseEventBean(x, y, EventType.ON_MOUSE_UP);
                fireMouseEvent(event);
            }
        });
    }

    private void initKeyboardListener() {
        addKeyboardListener(new KeyboardListener() {

            public void onKeyDown(Widget sender, char keyCode, int modifiers) {
                if (!KeyboardUtils.isSpecialKey((int) keyCode))
                    return;
                
                boolean isAltModifier = ((KeyboardListener.MODIFIER_ALT & modifiers) != 0);
                boolean isShiftModifier = ((KeyboardListener.MODIFIER_SHIFT & modifiers) != 0);
                boolean isCtrlModifier = ((KeyboardListener.MODIFIER_CTRL & modifiers) != 0);
                KeyboardEventBean eventDown = new KeyboardEventBean((int)keyCode,
                                                                    keyCode,
                                                                    isCtrlModifier,
                                                                    isAltModifier,
                                                                    isShiftModifier,
                                                                    EventType.ON_KEY_DOWN);
                fireKeyboardEvent(eventDown);
            }

            public void onKeyPress(Widget sender, char keyCode, int modifiers) {
                if (KeyboardUtils.isSpecialKey((int) keyCode))
                    return;
                boolean isAltModifier = ((KeyboardListener.MODIFIER_ALT & modifiers) != 0);
                boolean isShiftModifier = ((KeyboardListener.MODIFIER_SHIFT & modifiers) != 0);
                boolean isCtrlModifier = ((KeyboardListener.MODIFIER_CTRL & modifiers) != 0);
                KeyboardEventBean eventDown = new KeyboardEventBean((int)keyCode,
                                                                    keyCode,
                                                                    isCtrlModifier,
                                                                    isAltModifier,
                                                                    isShiftModifier,
                                                                    EventType.ON_KEY_TYPE);
                fireKeyboardEvent(eventDown);
//                KeyboardEventBean eventUp = new KeyboardEventBean((int)keyCode,
//                                                                  keyCode,
//                                                                  isCtrlModifier,
//                                                                  isAltModifier,
//                                                                  isShiftModifier,
//                                                                  EventType.ON_KEY_UP);
//                fireKeyboardEvent(eventUp);
            }

            public void onKeyUp(Widget sender, char keyCode, int modifiers) {
                if (!KeyboardUtils.isSpecialKey((int) keyCode))
                    return;
                boolean isAltModifier = ((KeyboardListener.MODIFIER_ALT & modifiers) != 0);
                boolean isShiftModifier = ((KeyboardListener.MODIFIER_SHIFT & modifiers) != 0);
                boolean isCtrlModifier = ((KeyboardListener.MODIFIER_CTRL & modifiers) != 0);
                KeyboardEventBean eventUp = new KeyboardEventBean((int)keyCode,
                                                                  keyCode,
                                                                  isCtrlModifier,
                                                                  isAltModifier,
                                                                  isShiftModifier,
                                                                  EventType.ON_KEY_UP);
                fireKeyboardEvent(eventUp);
            }
        });
    }

    public void addImage(Image img, int left, int top) {
        this.container.add(img, left, top);
    }

    public void addImagePanelListener(ImagePanelListener listener) {
        this.listeners.add(listener);
    }

    public void removeImagePanelListener(ImagePanelListener listener) {
        this.listeners.remove(listener);
    }

    private void fireMouseEvent(MouseEventBean event) {
        for (Iterator it = this.listeners.iterator(); it.hasNext();) {
            ImagePanelListener aListener = (ImagePanelListener) it.next();
            aListener.onMouseEvent(event);
        }
    }

    private void fireKeyboardEvent(KeyboardEventBean event) {
        for (Iterator it = this.listeners.iterator(); it.hasNext();) {
            ImagePanelListener aListener = (ImagePanelListener) it.next();
            aListener.onKeyboardEvent(event);
        }
    }

}
